export function getStyle (obj, property) {
  return document.defaultView.getComputedStyle ? parseInt(document.defaultView.getComputedStyle(obj, false)[property]) : parseInt(obj.currentStyle[property])
}
let dom = {
  addClass: function (className) {
    if (typeof className === 'undefined') {
      return this
    }
    var classes = className.split(' ')
    for (var i = 0; i < classes.length; i++) {
      for (var j = 0; j < this.length; j++) {
        // this[j].classList.add(classes[i])
        this[j].className = this[j].className + ' ' + classes[i]
      }
    }
    return this
  },
  removeClass: function (className) {
    var classes = className.split(' ')
    for (var i = 0; i < classes.length; i++) {
      var reg = new RegExp('(\\s|^)' + classes[i] + '(\\s|$)')
      for (var j = 0; j < this.length; j++) {
        // this[j].classList.remove(classes[i])
        this[j].className = this[j].className.replace(reg, ' ')
      }
    }
    return this
  },
  hasClass: function (className) {
    if (!this[0]) return false
    // else return this[0].classList.contains(className)
    else return this[0].className.indexOf(className) !== -1
  },
  toggleClass: function (className) {
    var classes = className.split(' ')
    for (var i = 0; i < classes.length; i++) {
      for (var j = 0; j < this.length; j++) {
        if (this[j].hasClass(classes[i])) {
          this[j].removeClass(classes[i])
        } else {
          this[j].addClass(classes[i])
        }
      // this[j].classList.toggle(classes[i])
      }
    }
    return this
  },
  attr: function (attrs, value) {
    if (arguments.length === 1 && typeof attrs === 'string') {
      // Get attr
      if (this[0]) return this[0].getAttribute(attrs)
      else return undefined
    } else {
      // Set attrs
      for (var i = 0; i < this.length; i++) {
        if (arguments.length === 2) {
          // String
          this[i].setAttribute(attrs, value)
        } else {
          // Object
          for (var attrName in attrs) {
            this[i][attrName] = attrs[attrName]
            this[i].setAttribute(attrName, attrs[attrName])
          }
        }
      }
      return this
    }
  },
  removeAttr: function (attr) {
    for (var i = 0; i < this.length; i++) {
      this[i].removeAttribute(attr)
    }
    return this
  }
}
export { dom }

// 获取url中的参数   传入参数名name
export const getUrlParam = function (name) {
  var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)'); // 构造一个含有目标参数的正则表达式对象
  var r = window.location.search.substr(1).match(reg); // 匹配目标参数
  if (r != null) return unescape(r[2]); return null; // 返回参数值
}
export function uuid (len, radix) {
  var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('')
  var uuid = [], i
  radix = radix || chars.length

  if (len) {
    // Compact form
    for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix]
  } else {
    // rfc4122, version 4 form
    var r

    // rfc4122 requires these characters
    uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-'
    uuid[14] = '4'

    // Fill in random data.  At i==19 set the high bits of clock sequence as
    // per rfc4122, sec. 4.1.5
    for (i = 0; i < 36; i++) {
      if (!uuid[i]) {
        r = 0 | Math.random() * 16
        uuid[i] = chars[(i === 19) ? (r & 0x3) | 0x8 : r]
      }
    }
  }

  return uuid.join('')
}
